package com.example.agriculture.controller;

import com.example.agriculture.entity.*;
import com.example.agriculture.service.OrderFormService;
import com.example.agriculture.service.ProducerService;
import com.example.agriculture.service.ProductService;
import com.example.agriculture.service.ProductTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Producer)表控制层
 *
 * @author makejava
 * @since 2022-09-12 15:24:18
 */
@Controller
public class ProducerController {
    @Resource
    private OrderFormService orderFormService;
    @Resource
    private ProductTypeService productTypeService;
    @Resource
    private ProductService productService;



    //种植户后台管理主页面
    @GetMapping("/PBmsIndex")
    public String PBmsIndex(){
        return "BMS/producermanager/p_index";
    }

    //种植户添加商品类型
    @GetMapping("/PProductTypeAdd")
    public String PProductTypeAdd(){
        return "BMS/producermanager/p_product_type_add";
    }
    //种植户添加产品
    @GetMapping("/PProductAdd")
    public String PProductAdd(){
        return "BMS/producermanager/p_product_add";
    }
    @GetMapping("/PPerson")
    public String PPerson(){
        return "BMS/producermanager/p_person_xinxi";
    }
    @GetMapping("/PProducerUpdatePWD")
    public String PProducerUpdatePWD(){
        return "BMS/producermanager/p_producerupdate_pwd";
    }
    @GetMapping("/PProducerexit")
    public String PProducerexit(HttpSession session){
        session.invalidate();
        return "redirect:admin";

    }
    @PostMapping("/PProductNewPwd")
    public String PProductNewPwd(String producerPassword,String producerNewpwd2,Integer producerId,HttpSession session){
        System.out.println(producerPassword);
        System.out.println(producerNewpwd2);
        if (producerPassword.equals(producerNewpwd2)==false){
            return  "redirect:PProducerUpdatePWD";
        }
        producerService.produceruppwd(producerPassword,producerId);
        session.invalidate();




        return "redirect:admin";

    }
    //种植户新增商品类型
//    @GetMapping("/PProductAdd"){
//        r
//    }
    //种植户查看产品类型
    @GetMapping("/PProductTypeInfor")
    public ModelAndView PProductTypeInfor(@RequestParam(defaultValue = "1") Integer page){
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.setViewName("BMS/producermanager/p_product_type_infor");
        int count = productTypeService.count()/5;
        List<ProductType> productTypes = productTypeService.queryAllByLimit((page-1)*5,5);
        modelAndView.addObject("productTypeInfor",productTypes);
        modelAndView.addObject("count",count+1);
        modelAndView.addObject("page",page);
        return modelAndView;
    }

    //种植户删除商品类型
    @GetMapping("/ProductTypeDel")
    @ResponseBody
    public Map<String, Object> ProducerDel(Integer id){
        Boolean R=productTypeService.deleteById(id);
        Map<String,Object>map = new HashMap<>();
        if (R){
            map.put("res","ok");
            map.put("msg","删除成功");
            return map;
        }
        map.put("res","false");
        map.put("msg","删除失败");
        return map;
    }
    //种植户新增商品类型
    @PostMapping("/ProductTypeAdd")
    public String ProductTypeAdd(ProductType productType){
//        ProductType query =productTypeService.insert()
//        System.out.println(query);
//        if(query ==null){
            int PTypeAdd= productTypeService.producttype(productType);
            System.out.println(PTypeAdd);
            if (PTypeAdd!=0){

                return "redirect:PProductTypeInfor";
            }
//        }
        return "/BMS/producermanager/p_product_type_add";
    }
//种植户更新商品类型
    @GetMapping("/produttypeupdate")
    public ModelAndView ProductTypeupdate(Integer id){
        ProductType productType = productTypeService.queryById(id);
        System.out.println(productType);
        ModelAndView M = new ModelAndView();
        M.setViewName("/BMS/producermanager/p_product_tupe_update");
        M.addObject("ProductTypeupd",productType);
        return M;

    }
    //种植户提交商品类型更新表单
    @PostMapping("/ProductTypeup")
    public ModelAndView ProductTypeupdatePost(ProductType productType){
        ModelAndView modelAndView = new ModelAndView();
        ModelAndView modelAndView2 = new ModelAndView();
        modelAndView2.setViewName("/BMS/producermanager/p_product_tupe_update");
        modelAndView.setViewName("redirect:PProductTypeInfor");
      int up = productTypeService.typeupdate(productType);
        if (up!=0){
            return modelAndView;
        }
      return modelAndView2;

    }
    //种植户商品类型模糊查询
@PostMapping("productTypeSelect")
public ModelAndView productTypeSel(String typename,@RequestParam(defaultValue = "1") Integer page){
    System.out.println(typename);
    ModelAndView modelAndView =new ModelAndView();
    modelAndView.setViewName("/BMS/producermanager/p_product_type_infor");
    List<ProductType> productTypes = productTypeService.queryAll((page-1)*5,5,typename);
    int count =  productTypeService.countLimit(typename)/5;
    modelAndView.addObject("count",count+1);
    modelAndView.addObject("productTypeInfor",productTypes);
    modelAndView.addObject("page",page);
    return modelAndView;

}


    //种植户查看产品信息
    @GetMapping("/PProductInfor")
    public ModelAndView PProductInfor(@RequestParam(defaultValue = "1") Integer page, HttpSession session){
        Producer producer = (Producer) session.getAttribute("producer");
        ModelAndView modelAndView = new ModelAndView();
        String name=null;
        modelAndView.setViewName("BMS/producermanager/p_product_infor");
        List<Product> products = productService.queryAllByLimit(name,(page-1)*5,5,producer.getProducerId());
        int count = (products.size())/5;
        modelAndView.addObject("count",count+1);
        modelAndView.addObject("products",products);
        modelAndView.addObject("page",page);
        return modelAndView;
    }
//种植户查看订单
@GetMapping("/ProdutOrderInfor")
public ModelAndView POrderInfor(@RequestParam(defaultValue = "1") Integer page,HttpSession session){
    Producer producer = (Producer) session.getAttribute("producer");
    List<OrderForm> orderForms = orderFormService.queryAllToLimit((page-1)*5,5,producer.getProducerId());
    System.out.println("++++++++++++++++++++++++++++++++++++++++++++++");
    System.out.println(orderForms);
    int count = orderFormService.count()/10;
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("BMS/producermanager/p_order_infor");
    modelAndView.addObject("count",count+1);
    modelAndView.addObject("orderForm",orderForms);
    modelAndView.addObject("page",page);
    return modelAndView;
}



    @PostMapping("/PProductAdd_main")
    public Object PProductAddMain(Product product,HttpSession session) {
        System.out.println(product);
        ModelAndView modelAndView = new ModelAndView();
        Producer producer = (Producer) session.getAttribute("producer");
        Producer producer1 = null;
        if (producer == null) {
            producer1=producer;
        }else {
            producer1 = new Producer();
        }
        product.setProductCreatTime(String.valueOf(product.getProductCreatTime()));
        product.setProducerNumber(producer.getProducerNumber());
        product.setProducerId(producer.getProducerId());
        product.setProducerName(producer.getProducerName());

        product.setProductState("未审核");
        product.setProductReleaseTime(" ");
        modelAndView.setViewName("BMS/producermanager/p_product_infor");
        String name=null;
        if (product!=null){
            productService.product(product);
            List<Product> products = productService.queryAllByLimit(name,0, 10,producer1.getProducerId());
            modelAndView.addObject("productsinfor", products);
            return "redirect:/PProductInfor";
        }
        else return "redirect:/PProductInfor";


    }

    //删除商品
    @GetMapping("/PDelProductInfor")
    @ResponseBody
    public String PDelProductInfor(Integer id){

        productService.deleteById(id);


        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("BMS/producermanager/p_product_infor");


        return "PProductInfor";
//        return "redirect:/PProductInfor";
    }



    //商品
    //管理员更新商品
    @GetMapping("/PupdatePuduct")
    public ModelAndView PupdatePuduct(Integer id){
        System.out.println(id);
        Product product = productService.queryById(id);
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(product);
        modelAndView.setViewName("BMS/producermanager/p_product_updata");
        modelAndView.addObject("productsinfor",product);
        return modelAndView;
    }
    @PostMapping("PostPupdatePuduct")
    public String PostPupdatePuduct(Product product){
        System.out.println(product);
        ModelAndView modelAndView = new ModelAndView();
        int update = productService.update(product);
        System.out.println(update);
        modelAndView.setViewName("BMS/producermanager/p_product_infor");
//        List<Product> products = productService.queryAllByLimit(0,10);
//        modelAndView.addObject("productsinfor",products);
        return "redirect:/PProductInfor";
    }
    

    //种植户搜索商品信息
    @PostMapping("PSelectProductInfor")
    public ModelAndView PSelectProductInfor(String productname,@RequestParam(defaultValue = "1") Integer page,HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        Producer producer = (Producer) session.getAttribute("producer");
        modelAndView.setViewName("BMS/producermanager/p_product_infor");
        List<Product> products = productService.queryAllByLimit(productname,(page-1)*5,5,producer.getProducerId());
        int count = (products.size())/5;
        modelAndView.addObject("page",page);
        modelAndView.addObject("count",count+1);
        modelAndView.addObject("products",products);
        return modelAndView;
    }

//    //图片添加
//    @Value("${web.upload-path}")
//    String path;
//    @PostMapping("/producerUpFile")
//    @ResponseBody
//    public String getUpFile(MultipartFile file) throws IOException {
//        String s = UUID.randomUUID().toString().replaceAll("-","");
//        String originalFilename = file.getOriginalFilename();
//        String type = originalFilename.substring(originalFilename.lastIndexOf("."));
//        System.out.println(originalFilename);
//        file.transferTo(new File(path+s+type));
//        return s+type;
//    }













    /**
     * 服务对象
     */
    @Resource
    private ProducerService producerService;

    @GetMapping("/reg")
    public String produce(){
        return "BMS/reg";
    }
    @PostMapping("/reg_main")
    public String GetRegMain (Producer producer){
        Producer producers = producerService.producer(producer.getProducerNumber(), producer.getProducerPassword());

                if (producer!=null){
                    System.out.println(producer);
                    if (producers==null){
                    if (producer.getProducerNumber().replace(" ","").length()!=0&&producer.getProducerPassword().replace(" ","").length()!=0){
                        int pro = producerService.producer(producer);
                        System.out.println(pro);
                        System.out.println(pro);
                        if (pro != 0){
                            return "redirect:/admin";
                        }
                }
           }
        }
        return "BMS/reg";
    }

}